$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});

if( $("#fileUrl").val() ) {

	$(".listImg").html(`
	<div class="closeBtn">
		<img class="upImg" src="${$("#fileUrl").val()}" />
		<span class="iconclose close2" ><img src="../../../img/close.png"></span>
		</div>
	`)
}

layui.use(['upload', 'layer'], function() {
	var $ = layui.jquery
		, upload = layui.upload
		, layer = layui.layer;
	var uploadInst = upload.render({
		elem: '#test1'
		, url: '/common/sysFile/upload' // 实际使用时改成您自己的上传接口即可。
		, multiple: true
		, before: function (obj) {
			layer.load();
		}
		, done: function (res) {
			if (res.code == 0) {
				layer.closeAll();

				$("#fileUrl").val(res.data)
				$(".listImg").html(`
					<div class="closeBtn">
						<img class="upImg" src="${res.data}" />
						<span class="iconclose close2"><img src="../../../img/close.png"></span>
					</div>`)

			}
		}
		, error: function (res) {
			layer.closeAll();
			layer.msg(res.msg)
		}
	});
})

function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/zykTypeResources/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入名字"
			}
		}
	})
}

$(document).on('click', '.close2', function() {
	$(".closeBtn").remove();
	$("#fileUrl").val('')
});